import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.Enumeration;

/**
 * @Author 86152
 * @create 2024/3/18 14:56
 */
@WebServlet("/hts")
public class HttpTestServiet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String  method = req.getMethod();
        String requestURL = req.getRequestURI().toString();
        String requestURI = req.getRequestURI();
        String contextPath = req.getContextPath();


        System.out.println("请求方式:" + method);  //获取请求方式
        System.out.println("完整请求URL:" + requestURI);
        System.out.println("请求资源URI:" + requestURI);
        System.out.println("上下文路径:" + contextPath);


         Enumeration<String> headerNames = req.getHeaderNames();

         while (headerNames.hasMoreElements()){
             String hederName = headerNames.nextElement();
             System.out.println("Header Name: " + hederName);
             String headerValue = req.getHeader(hederName);
             System.out.println("Header Value: " + headerValue);
         }
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String  method = req.getMethod();
        System.out.println("请求方式:" + method);
        System.out.println("HttpTestServlet的doPost方法被执行了");
    }


    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String  method = req.getMethod();
        System.out.println("请求方式:" + method);
        System.out.println("HttpTestServlet的doPut方法被执行了");
    }


    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String  method = req.getMethod();
        System.out.println("请求方式:" + method);
        System.out.println("HttpTestServlet的doDelete方法被执行了");
    }
}
